import {Component, OnInit} from '@angular/core';
import {TranslateService} from '@ngx-translate/core';
import {NavigationCancel, NavigationEnd, NavigationError, NavigationStart, Router} from '@angular/router';
import {EventService} from './pages/basic/commons/event.service';
import {ConfirmationService} from 'primeng/api';

@Component({
    selector: 'app-root',
    templateUrl: 'app.component.html',
    providers: [ ConfirmationService ]
})
export class AppComponent implements OnInit {

    private loading: boolean = false;

    constructor(
        public translate: TranslateService,
        public router: Router,
        public eventService: EventService) {
    }

    ngOnInit(): void {
        this.translate.addLangs(['zh_CN', 'zh_TW', 'en_US']);
        this.translate.setDefaultLang('zh_CN');

        const browserLanguage: string = this.translate.getBrowserLang();
        this.translate.use(browserLanguage.match(/CN|US/) ? browserLanguage : 'zh_CN');

        this.eventService.showGlobalLoading.subscribe(value => {
            this.loading = value;
        });

        this.router.events.subscribe(event => {
            if (event instanceof NavigationStart) {
                this.eventService.showGlobalLoading.next(true);
            }
            if (event instanceof NavigationEnd || event instanceof NavigationError || event instanceof NavigationCancel) {
                this.eventService.showGlobalLoading.next(false);
            }
        });
    }
}
